/**
 * Created by WT-SUN on 2017/4/15.
 */
$(function () {
    $('#phyDataTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/phyData/phyTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc',
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'userPhy', title: '姓名', width: 100, formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'  onclick='infoPhyData(\""+row.id+"\");'>"+row.userPhy+"</a>";
            }},
            {
                field: 'CompanyName',
                title: '所属部门',
                width: 100,
                formatter: function (value, row, index) {
                    var name = '';
                    if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                        for (var index in row.userDepartmentList) {
                            if (row.userDepartmentList[index].deptName != null) {
                                name += ',' + row.userDepartmentList[index].deptName;
                            }
                        }
                        return name.substring(1);
                    } else {
                        return '';
                    }
                }
            },
            {field: 'batch', title: '体检批次', width: 100},
            {field: 'yearAnnual', title: '年度', width: 100},
            {field: 'planType', title: '体检类别', width: 100},
            {field: 'physicalDate', title: '体检日期', width: 100},
            {field: 'conclusion', title: '体检结论', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.phyConclusions && row.phyConclusions.length > 0) {
                    for (var index in row.phyConclusions) {
                        if (row.phyConclusions[index].conName != null) {
                            name += ',' + row.phyConclusions[index].conName;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'userName', title: '登记人', width: 100},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}
        ]]
    });
    queryComboData();
    $('#btn_phyData_add').on('click',function () {
        addPhyData();
    });
    $('#btn_phyData_delete').on('click',function () {
        deletePhyData();
    });
    $('#btn_phyData_edit').on('click',function () {
        editPhyData();
    });
    /**
     * 高级查询
     */
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#phyDataTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
    });
    queryYearthList("yearAnnual");
    queryDepartmentTree();
    var code1=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_CONCLUSION;
    queryComboData(code1,'conclusion');
    var code2=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_TYPE;
    queryComboData(code2,'medicalCategory');
    /**
     * 绑定人员选择事件
     */
    $("#searchAddUser").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }]
    });
});
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#searchAddUser").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
};
/**
 * 年度
 * @param id
 */
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text',
            editable:false
        }
    );
}
function queryComboData(code,id) {
    var comId='#'+id;
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(comId).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
            });
        }
    });
}
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchDept',result);
            deptCom('searchAddDept',result);
        }
    });
}
function deptCom(id,result) {
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            prompt:'请选择',
            loadFilter: function (data) {
                data.unshift(EMPTY_OPTION);
                return data;
            },
        }
    );
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#phyDataTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#phyDataTb').datagrid('options').queryParams = queryParams;
    $("#phyDataTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/phyData/phyTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['1150px', '80%'],
        content: content,
        callBack:function(data){
            if (data){
                reLoadData();//重新加载表格
            }
        }
    });
}
/**
 * 打开新建
 */
function addPhyData() {
    var title = '新增体检数据';
    var content = ROOT_PATH + '/occupationalHealth/phyData/add.shtml?resourceId=' + $.getQueryParam('resourceId');
    openLayers(title, content);
}
/**
 * 删除
 */
function deletePhyData() {
    var rows = $('#phyDataTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/phyData/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#phyDataTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editPhyData() {
    var rows = $('#phyDataTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的数据！");
        return
    }
    var id = rows[0].id;
    var title = '编辑体检数据';
    var content = ROOT_PATH + '/occupationalHealth/phyData/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=edit';
    openLayers(title, content);
}

function infoPhyData(id) {
    var title = '体检数据详情';
    var content = ROOT_PATH + '/occupationalHealth/phyData/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=info';
    openLayers(title, content);
}

